package com.oigx.lookmu.core;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;

public class ShowLineUtil {

    private static boolean checkAndSetLine(Project project){
        SettingsState settingsState = SettingsState.getInstance();
        if (settingsState.bookList==null||settingsState.bookList.isEmpty()||settingsState.bookList.size()<=settingsState.fileIndex){
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                statusBar.setInfo("请选中book");
            }
            return false;
        }
        if (settingsState.chapterIndex<0){
            settingsState.chapterIndex = 0;
        }
        if (settingsState.chapterList==null||settingsState.chapterList.isEmpty()||settingsState.chapterList.size()<=settingsState.chapterIndex){
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                statusBar.setInfo("请选中chapter");
            }
            return false;
        }
        TxtBook txtBook = settingsState.bookList.get(settingsState.fileIndex);
        //System.out.println(txtBook.getBookName());
        TxtChapter txtChapter = settingsState.chapterList.get(settingsState.chapterIndex);
        //System.out.println(txtChapter.getTitleName());
        if (settingsState.readLineList==null||settingsState.readLineList.isEmpty()){
            System.out.println("readChapter");
            settingsState.readLineList = TxtReaderUtil.readChapter(txtBook.getFilePath(), txtChapter.getStartLine(), txtChapter.getEndLine(),
                    txtBook.getCharsetName(), settingsState.lineMaxChar);
            if (settingsState.readLineList.isEmpty()){
                return false;
            }
        }
        return true;
    }


    public static void nextLine(Project project){
        SettingsState settingsState = SettingsState.getInstance();
        if (!checkAndSetLine(project)){
            return;
        }
        settingsState.readLine++;
        if (settingsState.readLineList.size()>settingsState.readLine){
            System.out.println("show:"+settingsState.readLineList.get(settingsState.readLine));
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                statusBar.setInfo(settingsState.readLineList.get(settingsState.readLine));    //readLine 读之后再加1
            }
        } else {
            System.out.println("next chapter...");
            settingsState.chapterIndex ++ ;
            settingsState.readLine = 0;
            settingsState.readLineList = null;
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                if (settingsState.chapterIndex<settingsState.chapterList.size()){
                    statusBar.setInfo(settingsState.chapterList.get(settingsState.chapterIndex).getTitleName());
                } else {
                    statusBar.setInfo("没有更多章节");
                }
            }
        }
    }


    public static void prevLine(Project project){
        SettingsState settingsState = SettingsState.getInstance();
        if (!checkAndSetLine(project)){
            return;
        }
        settingsState.readLine--;
        if (settingsState.readLine>=0 && settingsState.readLineList.size()>settingsState.readLine ){
            System.out.println("show:"+settingsState.readLineList.get(settingsState.readLine));
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                statusBar.setInfo(settingsState.readLineList.get(settingsState.readLine));
            }
        } else {
            System.out.println("pre chapter...");
            settingsState.readLine = 0;
            settingsState.readLineList = null;
            StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
            if (statusBar != null){
                if (settingsState.chapterIndex>0){
                    settingsState.chapterIndex -- ;
                    statusBar.setInfo(settingsState.chapterList.get(settingsState.chapterIndex).getTitleName());
                } else {
                    statusBar.setInfo("已是开始章节");
                }
            }
        }
    }

}
